<?php

declare(strict_types=1);

namespace app\yperp\command;

use DateTime;
use Facebook\WebDriver\Exception\NoSuchWindowException;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Fastposter\Client as FastposterClient;
use think\console\Command;
use think\console\Input;

use think\console\Output;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Stream;
use GuzzleHttp\Psr7\Utils;
use InvalidArgumentException;
use RuntimeException;
use think\Db;
use think\facade\Cache;

class suosi4 extends Command
{

    protected $output;

    protected $input;
    protected $driver;
    protected $windows = [];
    protected $cookies = [];
    protected $token = 'Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2luZm8iOiIlN0IlMjJhdmF0YXIlMjIlM0ElMjJodHRwcyUzQSUyRiUyRndld29yay5xcGljLmNuJTJGd3dwaWMzYXolMkY5OTU3ODZfTnc0YXFZZEZTamFHbmpVXzE3MTYyNzc3MzMlMkYwJTIyJTJDJTIyY29ycElkJTIyJTNBJTIyd3cyYjFhOGVkZGIzMjcyZmZiJTIyJTJDJTIyaW5kdXN0cnlUeXBlJTIyJTNBJTIybXl0eGwlMjIlMkMlMjJsb2dpblR5cGUlMjIlM0ElMjJub3JtYWwlMjIlMkMlMjJtZW1iZXJJZCUyMiUzQTE3NjA5Mjg5MjA1MjY1ODAxMTMlMkMlMjJtZW1iZXJOYW1lJTIyJTNBJTIyJUU2JTlEJThFJUU0JUI4JTk2JUU1JUI5JUIzJTIyJTJDJTIybW9iaWxlJTIyJTNBJTIyMTg5ODExNDAxMDElMjIlMkMlMjJvcmdJZCUyMiUzQTEwMDAxMCUyQyUyMm9yZ05hbWUlMjIlM0ElMjIlRTklOTUlQkYlRTglOTklQjklRUYlQkMlODglRTYlOTklQkElRTYlOTglOTMlRTUlQUUlQjYlRUYlQkMlODklMjIlMkMlMjJvcmdUeXBlJTIyJTNBJTIyZ2VuZXJhbCUyMiUyQyUyMnJlZ2lvbkNvZGUlMjIlM0ElMjIwMDAwMDAwMDAwMDAwMDAwMDAlMjIlMkMlMjJzaG9ydE5hbWUlMjIlM0ElMjIlRTYlOTklQkElRTYlOTglOTMlRTUlQUUlQjYlMjIlMkMlMjJ1c2VySWQlMjIlM0ExNzYwOTI4OTIwNTI2NTgwMTEzJTJDJTIydXNlck5hbWUlMjIlM0ElMjIlRTYlOUQlOEUlRTQlQjglOTYlRTUlQjklQjMlMjIlMkMlMjJ1c2VyVHlwZSUyMiUzQSUyMmVtcGxveWVlJTIyJTdEIiwidXNlcl9uYW1lIjoiMTAwMDEwOzE3NjA5Mjg5MjA1MjY1ODAxMTM7ZW1wbG95ZWU7bm9ybWFsIiwib3JnX2lkIjoxMDAwMTAsInNjb3BlIjpbImFsbCJdLCJleHAiOjE3MjQzNzY1MzYsImp0aSI6IjBjZTlhNmQ3LWIwNDgtNDlmZS05YjkwLTljOWM3YzlhODNlNCIsImNsaWVudF9pZCI6InByb2R1Y3Rpb24ifQ.oELIeFt774ZgV-NDlJarV3uC4iOM9tyc7OVeL_cHUSL6eQpQP5Xxpqns_CWuBLfRaimxrR6PLw-YRb421HIxc71z5UoEXaLTjANN0ly4EMwLpbR6IYyg02w6cetYg4lSwM4K98j0QmW5u3K-L2_YK5LwJZAP-XfapfUBwSNrh6A';

    protected function configure()
    {
        // 指令配置
        $this->setName('suosi4')
            ->setDescription('批量更新群标签');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $this->input = $input;

        $this->output = $output;
        try {

            $this->go();

        } catch (\Throwable $th) {
            dump($th->getMessage());
            dump($th->getTraceAsString());
        }
    }

    function require($url = '', $body, $method = 'POST', $headers = [])
    {
        $client = new Client(['verify' => env('root_path') . 'application\yperp\common\cacert.pem',]);

        $headers['authorization'] = $this->token;
        $headers['content-type'] = 'application/json;charset=utf-8';

        $body = json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        dump($body);
        $request = new Request($method, $url, $headers, $body);
        $res = $client->sendAsync($request)->wait();
        $res = $res->getBody()->getContents();
        return json_decode($res, true);
    }
    function go()
    {
        $data = json_decode($this->json_data, true);
        $group_tags = $this->getTagsByGroupId('1794902491749605377', );//芜湖门店
        // dump($group_tags);
        $proens_tags = $this->getTagsByGroupId('1794902432265179137');//芜湖人员
        die;
        foreach ($data as $value) {
            try {
                //code...

                dump($value);
                $response = $this->require('https://m.suosishequ.com/gateway/crm/web/label/label/delete', ['id' => $group_tags['id']]);
                dump(['dep_name' => $response]);

                $response = $this->require('https://m.suosishequ.com/gateway/crm/web/label/label/delete', ['id' => $proens_tags['id']]);
                dump(['username' => $response]);

            } catch (\Throwable $th) {
                //throw $th;
            }
        }


    }

    private function getTagsByGroupId($group_id)
    {
        $url = 'https://m.suosishequ.com/gateway/crm/web/label/label/findPageList?groupId=' . $group_id . '&pageNo=1&pageSize=400';
        $response = $this->require($url, [], 'GET');
        if (isset($response['data']['dataSource'])) {

            $tags = [];
            foreach ($response['data']['dataSource'] as $targ_group) {
                // $tags[$targ_group['labelName']] = $targ_group['id'];
                // $tags[] = $targ_group['id'];

                $this->require('https://m.suosishequ.com/gateway/crm/web/label/label/delete', ['id' => $targ_group['id']]);
            }
            return $tags;
        }
        return [];
    }

    private $json_data = '[
        {"id":"13","dep":"芜湖","dep_type":"冰洗","dep_name":"苏宁安庆人民路云珠广场店","username":"王丽1","phone":"17309662959"},
        {"id":"14","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖苏宁步行街店","username":"王燕","phone":"13855356700"},
        {"id":"15","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖苏宁大润发店","username":"周娟1","phone":"17755346350"},
        {"id":"16","dep":"芜湖","dep_type":"冰洗","dep_name":"马鞍山五星电器店","username":"胡英","phone":"18255529800"},
        {"id":"267","dep":"芜湖","dep_type":"冰洗","dep_name":"安庆人民路店","username":"黄金萍","phone":"18955603201"},
        {"id":"268","dep":"芜湖","dep_type":"冰洗","dep_name":"安庆五星电器建设路店","username":"刘仁荣","phone":"15212903021"},
        {"id":"269","dep":"芜湖","dep_type":"冰洗","dep_name":"安庆五星电器吾悦广场店","username":"吴俊","phone":"13033165781"},
        {"id":"270","dep":"芜湖","dep_type":"冰洗","dep_name":"苏宁安庆人民路云珠广场店","username":"丁春明","phone":"13865113883"},
        {"id":"271","dep":"芜湖","dep_type":"冰洗","dep_name":"苏宁安庆人民路云珠广场店","username":"张爱琴","phone":"15155481238"},
        {"id":"272","dep":"芜湖","dep_type":"冰洗","dep_name":"安庆南翔电器","username":"丁翠芳","phone":"13966438369"},
        {"id":"273","dep":"芜湖","dep_type":"冰洗","dep_name":"安庆桐城同安路店","username":"胡海燕","phone":"13095568668"},
        {"id":"274","dep":"芜湖","dep_type":"冰洗","dep_name":"安庆桐城同安路店","username":"张智","phone":"15255650287"},
        {"id":"275","dep":"芜湖","dep_type":"冰洗","dep_name":"安庆敬平家电商行","username":"汪翠霞","phone":"18900561215"},
        {"id":"276","dep":"芜湖","dep_type":"冰洗","dep_name":"安庆恒大易家广场店","username":"杨君莉","phone":"13866056838"},
        {"id":"277","dep":"芜湖","dep_type":"冰洗","dep_name":"安庆苏宁欧尚店","username":"曹丽萍","phone":"13605567303"},
        {"id":"278","dep":"芜湖","dep_type":"冰洗","dep_name":"安庆怀宁振华家电","username":"江守文","phone":"18855604425"},
        {"id":"279","dep":"芜湖","dep_type":"冰洗","dep_name":"安庆潜山台联会财富电器","username":"许志红","phone":"13866608267"},
        {"id":"280","dep":"芜湖","dep_type":"冰洗","dep_name":"池州商之都店","username":"洪丽华","phone":"15656651917"},
        {"id":"281","dep":"芜湖","dep_type":"冰洗","dep_name":"池州苏宁长江路店","username":"程卫珍","phone":"18956615227"},
        {"id":"282","dep":"芜湖","dep_type":"冰洗","dep_name":"池州宏达家电(池州宏达)","username":"汪秀丽","phone":"18056665506"},
        {"id":"283","dep":"芜湖","dep_type":"冰洗","dep_name":"池州国岭商贸","username":"杨晓红","phone":"13156678868"},
        {"id":"284","dep":"芜湖","dep_type":"冰洗","dep_name":"池州苏宁大润发店","username":"徐红霞","phone":"18356606927"},
        {"id":"285","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖南京新百大厦","username":"郭霞","phone":"13955360038"},
        {"id":"286","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖南京新百大厦","username":"张婧","phone":"18949410881"},
        {"id":"287","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖南陵舒雅家电","username":"戴玉琼","phone":"13855339233"},
        {"id":"288","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖苏宁步行街店","username":"董怀平","phone":"15385602976"},
        {"id":"289","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖苏宁广场店","username":"刘云","phone":"13855336967"},
        {"id":"290","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖苏宁广场店","username":"李长凤","phone":"18055397788"},
        {"id":"291","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖苏宁欧尚店","username":"郑辰","phone":"15755352287"},
        {"id":"292","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖苏宁北京中路大润发店","username":"胡云","phone":"18664946658"},
        {"id":"293","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖五星电器步行街店","username":"洪晓玲","phone":"18155361976"},
        {"id":"294","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖五星电器步行街店","username":"吴桢琳","phone":"13355538771"},
        {"id":"295","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖五星电器步行街店","username":"鲍明凤","phone":"13335539560"},
        {"id":"296","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖苏宁欧尚鸠江北路店","username":"黄蕾","phone":"13083262626"},
        {"id":"297","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖苏宁南京采购中心南陵店","username":"黄琴琴","phone":"13955388670"},
        {"id":"298","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖五星万镇通电器店","username":"陶小红","phone":"18755384525"},
        {"id":"299","dep":"芜湖","dep_type":"冰洗","dep_name":"芜湖无为五星万镇通电器店","username":"夏玉香","phone":"19965377096"},
        {"id":"300","dep":"芜湖","dep_type":"冰洗","dep_name":"宣城南陵弋江五星万镇通店","username":"苏佳楠","phone":"18855318200"},
        {"id":"301","dep":"芜湖","dep_type":"冰洗","dep_name":"黄山苏宁星光荟店","username":"孙爱环","phone":"17355948523"},
        {"id":"302","dep":"芜湖","dep_type":"冰洗","dep_name":"黄山苏宁星光荟店","username":"程建英","phone":"13855914622"},
        {"id":"303","dep":"芜湖","dep_type":"冰洗","dep_name":"黄山苏宁大润发店","username":"马红霞","phone":"13855914602"},
        {"id":"304","dep":"芜湖","dep_type":"冰洗","dep_name":"黄山百大彩电组","username":"唐红梅","phone":"13905593163"},
        {"id":"305","dep":"芜湖","dep_type":"冰洗","dep_name":"黄山徽州区苏宁零售云店","username":"郑海莲","phone":"13645595115"},
        {"id":"306","dep":"芜湖","dep_type":"冰洗","dep_name":"黄山歙县苏宁百花路店","username":"方惠燕","phone":"15212461600"},
        {"id":"307","dep":"芜湖","dep_type":"冰洗","dep_name":"马鞍山苏宁花山大润发店","username":"吴亚男","phone":"18255929800"},
        {"id":"308","dep":"芜湖","dep_type":"冰洗","dep_name":"马鞍山苏宁华联商厦店","username":"刘腊梅","phone":"13696562813"},
        {"id":"309","dep":"芜湖","dep_type":"冰洗","dep_name":"马鞍山苏宁华联商厦店","username":"吕红梅","phone":"18055569033"},
        {"id":"310","dep":"芜湖","dep_type":"冰洗","dep_name":"马鞍山苏宁欧尚店","username":"黄明带","phone":"18255503886"},
        {"id":"311","dep":"芜湖","dep_type":"冰洗","dep_name":"马鞍山五星电器店","username":"黄晓萍","phone":"18055544585"},
        {"id":"312","dep":"芜湖","dep_type":"冰洗","dep_name":"马鞍山苏宁解放路店","username":"陶海英","phone":"15555599700"},
        {"id":"313","dep":"芜湖","dep_type":"冰洗","dep_name":"马鞍山当涂县五星电器亚华店","username":"陈玲","phone":"15555539028"},
        {"id":"314","dep":"芜湖","dep_type":"冰洗","dep_name":"马鞍山苏宁当涂提署西路店","username":"吴小香","phone":"13615556760"},
        {"id":"315","dep":"芜湖","dep_type":"冰洗","dep_name":"宣城苏宁宣州大润发店","username":"孙欣","phone":"18158838357"},
        {"id":"316","dep":"芜湖","dep_type":"冰洗","dep_name":"宣城商之都店","username":"吴爱玲","phone":"17749081165"},
        {"id":"317","dep":"芜湖","dep_type":"冰洗","dep_name":"宣城郎溪五星万镇通","username":"陶红芳","phone":"13966238611"},
        {"id":"318","dep":"芜湖","dep_type":"冰洗","dep_name":"宣城苏宁广德大润发店","username":"刘传玲","phone":"13865395193"},
        {"id":"319","dep":"芜湖","dep_type":"冰洗","dep_name":"宁国苏宁大润发店","username":"张芮","phone":"18225360903"},
        {"id":"320","dep":"芜湖","dep_type":"冰洗","dep_name":"铜陵宏达家电铜陵店","username":"钱冬月","phone":"13965221659"},
        {"id":"321","dep":"芜湖","dep_type":"冰洗","dep_name":"铜陵宏达家电铜陵店","username":"戴红霞","phone":"15956227159"},
        {"id":"322","dep":"芜湖","dep_type":"冰洗","dep_name":"铜陵苏宁义安北路店","username":"胡云芳","phone":"13866511155"},
        {"id":"323","dep":"芜湖","dep_type":"冰洗","dep_name":"安徽百大铜陵分店","username":"陆飞兵","phone":"13956257116"},
        {"id":"324","dep":"芜湖","dep_type":"冰洗","dep_name":"京东五星铜陵店","username":"赵宝玲","phone":"15345629208"},
        {"id":"325","dep":"芜湖","dep_type":"冰洗","dep_name":"京东五星铜陵店","username":"陈小桃","phone":"13339122587"},
        {"id":"326","dep":"芜湖","dep_type":"冰洗","dep_name":"铜陵苏宁大润发店","username":"王宝英","phone":"13865625826"},
        {"id":"327","dep":"芜湖","dep_type":"冰洗","dep_name":"铜陵长江路京东店","username":"夏正芹","phone":"18110673296"},
        {"id":"328","dep":"芜湖","dep_type":"冰洗","dep_name":"铜陵八佰伴京东店","username":"胡芳华","phone":"15056211996"},
        {"id":"329","dep":"芜湖","dep_type":"冰洗","dep_name":"江苏汇银电器宁国百盛分公司","username":"许韵蓓","phone":"13856393330"},
        {"id":"330","dep":"芜湖","dep_type":"冰洗","dep_name":"江苏汇银电器泾县分公司","username":"陈明艳","phone":"13966164158"},
        {"id":"331","dep":"芜湖","dep_type":"冰洗","dep_name":"宁国市乐静达商贸","username":"李露萍","phone":"18326423557"},
        {"id":"332","dep":"芜湖","dep_type":"冰洗","dep_name":"广德新兴顺家电","username":"方启玲","phone":"13966168191"},
        {"id":"333","dep":"芜湖","dep_type":"冰洗","dep_name":"江苏汇银电器宁国大华分公司","username":"罗娟","phone":"13865318571"}
        ]';
}


